package tdh.xt.config;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tdh.xt.conditional.HibernateCondition;
import tdh.xt.conditional.MybatisCondition;

import javax.annotation.Resource;

/*
* @Package tdh.xt.config
* @Description 注入事务管理器Bean的配置类
* @Author RicarAN
* @Date 2021/6/15 15:58
* @Copyright (c) 2021,南京通达海科技股份有限公司 All Rights Reserved.
* @Version 1.0
* Modification History:
* Date                  Author      Version     Description
* ------------------------------------------------------------------
* 2021/6/15 15:58       RicarAN      1.0        注入事务管理器Bean的配置类
*
*/
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@ImportResource(locations = {"classpath:spring/applicationContext.xml", "classpath:spring/dataSource.xml"})
public class TransactionManagerConfig {

    /*
     * @description hibernate事务管理器
     * @param hibernateSessionFactory
     * @return org.springframework.transaction.PlatformTransactionManager 事务管理器
     * @author RicarAN
     * @version 1.0
     * @date 2021/6/15 15:59
     */
    @Bean(name = "transactionManager")
    @Conditional(HibernateCondition.class)
    @Resource(name = "hibernateSessionFactory")
    public PlatformTransactionManager getHibernateTransactionManager(LocalSessionFactoryBean hibernateSessionFactory) {
        HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
        hibernateTransactionManager.setSessionFactory(hibernateSessionFactory.getObject());
        return hibernateTransactionManager;
    }

    /*
     * @description Mybatis事务管理器
     * @param dataSource
     * @return org.springframework.transaction.PlatformTransactionManager 事务管理器
     * @author RicarAN
     * @version 1.0
     * @date 2021/6/15 16:00
     */
    @Bean(name = "transactionManager")
    @Conditional(MybatisCondition.class)
    @Resource(name = "dataSource")
    public PlatformTransactionManager getMybatisTransactionManager(BasicDataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }
}
